package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.mapper.AgentBonusGroupMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentBonusGroup;
import com.weixing.mall.provider.model.enums.ObligorRoleEnum;
import com.weixing.mall.provider.model.query.AgentBonusGroupQuery;
import com.weixing.mall.provider.model.vo.AgentBonusGroupVo;
import com.weixing.mall.provider.service.IAgentBonusGroupService;
import com.weixing.mall.provider.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代理商分红团队 服务实现类
 * </p>
 *
 * @author Small
 * @since 2020-03-12
 */
@Service
public class AgentBonusGroupServiceImpl extends ServiceImpl<AgentBonusGroupMapper, AgentBonusGroup> implements IAgentBonusGroupService {


    @Autowired
    private IAgentService agentService;

    @Override
    public AgentBonusGroup findByAgentId(Long id) {
        return getOne(new QueryWrapper<AgentBonusGroup>().eq("agent_id", id).eq("is_normal", IsEnableEnum.ENABLE.getValue()));
    }

    @Override
    public List<AgentBonusGroup> findByObliee(Long id) {
        QueryWrapper<AgentBonusGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("obligee_id1st", id).or().eq("obligee_id2nd", id);
        return list(wrapper);
    }

    @Override
    public boolean hasGroup(Long agentId) {
        QueryWrapper<AgentBonusGroup> wrapper = new QueryWrapper<AgentBonusGroup>();
//        wrapper.exists("select id from amc_agent_bonus_group where is_enable = 1 and   (agent_id = " + agentId + " or obligee_id1st = " + agentId
//                + " or obligee_id2nd = " + agentId + " or (obligor1st = " + agentId + " and obligor_role1st = 2))");

        wrapper.exists("select id from amc_agent_bonus_group where  (agent_id = " + agentId + " and (is_lv1enable = 1 OR is_lv2enable = 1))  " +
                " OR (obligee_id1st = " + agentId + " AND is_lv1enable = 1) " +
                " OR (obligee_id2nd = " + agentId + " AND is_lv2enable = 1) " +
                " OR (obligor1st = " + agentId + " AND is_lv1enable = 1 AND obligor_role1st = 2)");
        List<AgentBonusGroup> list = list(wrapper);
        if (CollUtil.isNotEmpty(list)) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<AgentBonusGroupVo> selectListOfPage(Page page, AgentBonusGroupQuery query) {
        IPage<AgentBonusGroupVo> agentBonusGroupVoIPage = baseMapper.selectListOfPage(page, query);
        List<AgentBonusGroupVo> records = agentBonusGroupVoIPage.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (AgentBonusGroupVo vo : records) {
                Agent agent = agentService.getById(vo.getAgentId());
                vo.setAgentLevel(agent.getAgentLevel());
                if (0 != vo.getObligeeId1st()) {
                    Agent oblee1 = agentService.getById(vo.getObligeeId1st());
                    if (null != oblee1) {
                        vo.setObligee1stLevel(oblee1.getAgentLevel());
                    }
                }
                if (0 != vo.getObligeeId2nd()) {
                    Agent obligee2 = agentService.getById(vo.getObligeeId2nd());
                    if (null != obligee2) {
                        vo.setObligee2ndLevel(obligee2.getAgentLevel());
                    }
                }
                if (0 != vo.getObligor1st() && ObligorRoleEnum.AGENT.getValue().equals(vo.getObligorRole1st())) {
                    Agent byId = agentService.getById(vo.getObligor1st());
                    if (null != byId) {
                        vo.setObligor1stLevel(byId.getAgentLevel());
                    }
                }
            }
        }
        return agentBonusGroupVoIPage;
    }

    @Override
    public AgentBonusGroup findByAgentIdAndNormalState(Long agentId, Integer isNormal) {
        QueryWrapper<AgentBonusGroup> wrapper = new QueryWrapper<AgentBonusGroup>();
        wrapper.eq("agent_id",agentId);
        wrapper.eq("is_normal",isNormal);
        return baseMapper.selectOne(wrapper);
    }

}
